package com.tianyang.yukuaifu.module.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.module.dialog.interfaces.ISourceChooseListener;

/**
 * Description：上传头像选择拍照还是相册
 */

public class IconSourceDialog {

    private TextView pickerTv, takePhotoTV, cancelTv;

    private Dialog aDialog;

    private ISourceChooseListener mListener;

    public IconSourceDialog(Context context) {
        initView(context);
    }

    public void setListener(ISourceChooseListener mListener) {
        this.mListener = mListener;
    }

    public void initView(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_icon_source_choose, null);
        aDialog = new Dialog(context, R.style.bottom_pop_menu);
        pickerTv = inflate.findViewById(R.id.pickerTv);
        takePhotoTV = inflate.findViewById(R.id.takePhotoTV);
        cancelTv = inflate.findViewById(R.id.cancelTv);
        View emptyView = inflate.findViewById(R.id.emptyView);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aDialog.dismiss();
            }
        });

        setUiBeforShow(context);

        aDialog.setContentView(inflate); // 把自定義view加上去
        aDialog.setCanceledOnTouchOutside(false);
        aDialog.setCancelable(false);
        aDialog.show();
    }

    public void setUiBeforShow(Context context) {
        pickerTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.pickerClick();
                }
                dismiss();
            }
        });
        takePhotoTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.takePhotoClick();
                }
                dismiss();
            }
        });
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void dismiss() {
        if (aDialog != null && aDialog.isShowing()) {
            aDialog.dismiss();
        }
    }
}
